#coding:utf-8


from Template import boofTemp

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET


class FuzzData:
    def __init__(self):
        self.unique_id = 1
        self.xml_list = []

    def walkData(self,root_node, level, result_list):
        #if root_node.attrib == {}:return
        try:
            temp_list = [level, root_node.tag, root_node.attrib]
        except:
            return
        result_list.append(temp_list)
        self.unique_id += 1

        # 遍历每个子节点
        children_node = root_node.getchildren()
        if len(children_node) == 0:
            return
        for child in children_node:
            self.walkData(child, level + 1, result_list)
        return


    def load_xml_file(self,path):
        level = 1
        tree = ET.parse(path).getroot()
        self.walkData(tree,level + 1,self.xml_list)


    def str_to_tmpe(self,func):
        func_str = ""
        if len(func) == 0:
            return None
        for f in func:
            func_str += f + "\r\n        "

        return boofTemp % func_str

    def boofuzz(self):
        if self.xml_list[0][1] != "fuzz":
            return None
        if self.xml_list[0][2]['tool_name'] != "boofuzz":
            return None
        
        func = []
        for x in self.xml_list[1:]:
            node_name = x[1]
            node_value = x[2]
            if node_value == {}:continue;
            if node_name == 'initialize':
                node_name = '''s_initialize("%s")''' % node_value['name']
            elif node_name == 'info':
                if node_value['value'][0] == "%" and node_value['value'][-1] == "%":
                    node_value['value'] = node_value['value'][2:-2]
                    node_name = '''s_%s(%s)''' % (node_value['key'], node_value['value'])
                else:
                    node_name = '''s_%s("%s")''' % (node_value['key'],node_value['value'])
            elif node_name == 'connect':
                params = ""
                if node_value['params'].find(",") > -1:
                    tmp_val = node_value['params'].split(',')
                    for val in tmp_val:
                        params += '''s_get("%s"),''' % val
                    params = params[:-1]
                else:
                    params = '''s_get(%s)''' % node_value['params']
                node_name = '''self.session_.connect(%s)''' % params

            func.append(node_name)
        return func # is list

    def zzuf(self):
        if self.xml_list[0][1] != "fuzz":
            return None
        if self.xml_list[0][2]['tool_name'] != "zzuf":
            return None
        func = []
        for x in self.xml_list[1:]:
            node_name = x[1]
            node_value = x[2]
            print(node_name)
            print(node_value)
            if node_name == "mutagen":
                tmp = ""
                for k in node_value:
                    tmp += k + "=" + node_value[k] + ","
                node_name = tmp[:-1]
            elif node_name == "data":
                if node_value.has_key('encode'):
                    pass
                    
                if node_value.has_key('type'):
                    pass


            elif node_name == "file":
                pass



            func.append(node_name)
        return func


if __name__ == "__main__":
    pass
    test = FuzzData()
    test.load_xml_file('/Users/mac/Fuzz/fconfuzz/Agent/Plugin/doc.xml')
    func = test.zzuf()
    # x = test.str_to_tmpe(func)
    # #print(x)
    # #exec(x)
    # with open("test.py`", "w") as f:
    #     f.write(x)
    #single_code = compile(x, '', 'single')
    # exec(single_code)